﻿@using UserStories.ViewModels.Projects;

@model EditSprintViewModel

@{
    ViewBag.Title = "Sprint";
}

<form action="@Url.Action("EditSprint")" method="POST">
    <input name="ProjectId" type="hidden" value="@Model.ProjectId"/>
    <input name="Id" type="hidden" value="@Model.Id"/>
    Review: <textarea name="Review">@Model.Review</textarea>
    <br />
    <div>
        <h3>User Stories</h3>
        <table>
            <tr>
                <th>Story</th>
                <th>Actions</th>
            </tr>
            <tbody data-bind="foreach: UserStories">
                <tr>
                    <td>
                        <select data-bind="options: $root.AllUserStories,
                                            optionsText: 'Description',
                                            optionsValue: 'Id',
                                            value: Id,
                                            optionsCaption: 'Select user story...',
                                            attr: { name: 'UserStories[' + $index() + '].Id' }" /></td>
                    <td><a href='#' data-bind='click: $parent.removeUserStory'>Delete</a></td>
                </tr>
            </tbody>
        </table>
        <div>
            <button data-bind='click: addUserStory'>Add user story</button>
        </div>
    </div>

    <input type="submit" value="Save" />
</form>

@section scripts
{
    <script type="text/javascript">
        var sprint = $.parseJSON('@Html.Raw(Json.Encode(Model))');

        function EditSprintViewModel(model) {
            var self = this;

            self.UserStories = ko.observableArray(model.UserStories);
            self.AllUserStories = ko.observableArray(model.AllUserStories);

            self.addUserStory = function() {
                self.UserStories.push({ Id: 0, Description: "" });
            };

            self.removeUserStory = function(userStory) {
                self.UserStories.remove(userStory);
            };
        }

        var vm = new EditSprintViewModel(sprint);
        ko.applyBindings(vm);
    </script>
}
